package com.ruoyi.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.admin.domain.Hospital;
import com.ruoyi.admin.mapper.HospitalMapper;
import com.ruoyi.admin.vo.DepartmentVO;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.DepartmentMapper;
import com.ruoyi.admin.domain.Department;
import com.ruoyi.admin.service.IDepartmentService;

/**
 * 科室信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-23
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private HospitalMapper hospitalMapper;


    /**
     * 查询科室信息
     *
     * @param deptId 科室信息主键
     * @return 科室信息
     */
    @Override
    public Department selectDepartmentByDeptId(Long deptId) {
        return departmentMapper.selectDepartmentByDeptId(deptId);
    }

    /**
     * 查询科室信息列表
     *
     * @param department 科室信息
     * @return 科室信息
     */
    @Override
    public List<Department> selectDepartmentList(Department department) {
        List list = departmentMapper.selectDepartmentList(department);
        for (Object obj : list) {
            System.out.println((Department) obj);
        }
        return departmentMapper.selectDepartmentList(department);
    }

    /**
     * 查询科室信息列表(用于科室主界面展示)
     *
     * @param department 科室信息
     * @return 科室信息集合
     */
    @Override
    public List<DepartmentVO> showAllDepartment(Department department) {

        List<Department> departments = departmentMapper.selectDepartmentList(department);
        List<DepartmentVO> list = new ArrayList<>();
        for (Department dep : departments) {
            DepartmentVO departmentVO = new DepartmentVO();
            Hospital hospital = hospitalMapper.selectHospitalByHospitalId(dep.getHospitalId());
            String hospitalName = hospital.getHospitalName();
            departmentVO.setUserName("实验补全");
            departmentVO.setDepartmentName(dep.getDepartmentName());
            departmentVO.setDepartPhone(dep.getDepartPhone());
            departmentVO.setCardNo(dep.getCardNo());
            departmentVO.setHospitalName(hospitalName);
            departmentVO.setDeptId(dep.getDeptId());
            departmentVO.setHospitalId(dep.getHospitalId());
            departmentVO.setDeptId(dep.getDeptId());
            departmentVO.setGainSharing(dep.getGainSharing());
            list.add(departmentVO);
        }
        return list;
    }

    /**
     * 新增科室信息
     *
     * @param department 科室信息
     * @return 结果
     */
    @Override
    public int insertDepartment(Department department) {
        department.setCreateTime(DateUtils.getNowDate());
        return departmentMapper.insertDepartment(department);


    }

    /**
     * 修改科室信息
     *
     * @param department 科室信息
     * @return 结果
     */
    @Override
    public int updateDepartment(Department department) {
        return departmentMapper.updateDepartment(department);
    }

    /**
     * 批量删除科室信息
     *
     * @param deptIds 需要删除的科室信息主键
     * @return 结果
     */
    @Override
    public int deleteDepartmentByDeptIds(Long[] deptIds) {
        return departmentMapper.deleteDepartmentByDeptIds(deptIds);
    }

    /**
     * 删除科室信息信息
     *
     * @param deptId 科室信息主键
     * @return 结果
     */
    @Override
    public int deleteDepartmentByDeptId(Long deptId) {
        return departmentMapper.deleteDepartmentByDeptId(deptId);
    }

    public List<Department> selectHospitalDept(Long hospitalId) {
        return departmentMapper.selectHospitalDept(hospitalId);
    }
}
